package com.yunbiao.ybbusstopplate.serialport.agreement;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.android.internal.util.ArrayUtils;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.LogUtils;
import com.yunbiao.ybbusstopplate.bean.SiteInfoBean;
import com.yunbiao.ybbusstopplate.utils.BytesUtils;
import com.yunbiao.ybbusstopplate.utils.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class LSPortAgreement extends BaseAgreement {
    private static final String TAG = "LSPortAgreement";
    private static LSPortAgreement lsPortAgreement;
    private final ExecutorService executorService;

    private LSPortAgreement(){
        byteBufferList = new LinkedList<>();
        executorService = Executors.newSingleThreadExecutor();

//        dataTool=new AgreementDataTool();
//        dataTool.setSHex("7e");
//        dataTool.setEHex("7e");
//        //获取数据
//        dataTool.setDataReceive(new AgreementDataTool.AgreementDataReceive() {
//            @Override
//            public void onDataReceive(byte[] datas) {
//                if (datas==null||datas.length<=0){
//                    return;
//                }
//
//                byte data = datas[1];
//                if(data != 0x00){
//                    return;
//                }
//
//                byte deviceNumber = datas[2];
//                if(deviceNumber != 0x04 && deviceNumber != 0x05){
//                    return;
//                }
//
//                choiseData(datas);
//            }
//        });
    }
    public static LSPortAgreement getInstance(){
        if (lsPortAgreement==null){
            lsPortAgreement=new LSPortAgreement();
        }
        return lsPortAgreement;
    }

    // 7e帧头
    // 01址址
    // 05协议
    // 12
    // 00
    // 00
    // 16
    // 7e


    private boolean isStartOrEnd(byte b){
        return b == (byte) 0x7e || b == (byte) 0x7f;
    }

    private boolean hasStart(String s){
        return s.startsWith("7e00") || s.startsWith("7f00");
    }

    private boolean hasEnd(String s){
        return s.endsWith("7e") || s.endsWith("7f");
    }

    private final StringBuilder stringBuilder = new StringBuilder();
    private final LinkedList<Byte> byteBufferList;
    public synchronized void anlzData(byte[] datas){
        String dataHex = BytesUtils.bytesToHex(datas);
        if(hasStart(dataHex) && hasEnd(dataHex)){
            Log.d(TAG, "anlzData: 包含指令头和指令尾");
            choiseData(datas);
        } else if(hasStart(dataHex)){
            Log.d(TAG, "anlzData: 只包含指令头");
            stringBuilder.setLength(0);
            stringBuilder.append(dataHex);
        } else if(hasEnd(dataHex)){
            Log.d(TAG, "anlzData: 包含指令尾");
            stringBuilder.append(dataHex);
            byte[] bytes = BytesUtils.hexToByteArray(stringBuilder.toString());
            stringBuilder.setLength(0);
            choiseData(bytes);
        } else {
            Log.d(TAG, "anlzData: 不包含指令头和指令尾");
            stringBuilder.append(dataHex);
        }
//        stringBuilder.append();
//        if(hasStart(stringBuilder.toString()) && hasEnd(stringBuilder.toString())){
//            byte[] bytes = BytesUtils.hexToByteArray(stringBuilder.toString());
//            stringBuilder.setLength(0);
//            choiseData(bytes);
//        }

//        dataTool.handleData(datas);



        /*for (int i = 0; i < datas.length; i++) {
            byte data = datas[i];
            if (data == (byte) 0x7e) {
                //如果缓冲区为空说明是起始位
                if (byteBufferList.isEmpty()) {
                    //判断下一位，如果超出length说明后面没了，如果没超出则判断是否还是7e，如果是7e说明是上一个包留下
                    if (i + 1 >= datas.length || datas[i + 1] == (byte) 0x7e) {
                        continue;
                    }
                    byteBufferList.add(data);
                }
                //如果缓冲区不为空说明是结束位
                else {
                    // TODO: 2023/3/23 此处有坑，如果包中间有7e，会直接截断，导致此包损坏
                    //加入结束位后复制到另一个List中，然后清除
                    byteBufferList.add(data);
                    LinkedList<Byte> byteList = new LinkedList<>(byteBufferList);
                    byteBufferList.clear();

                    byte[] bytes = new byte[byteList.size()];
                    for (int i1 = 0; i1 < bytes.length; i1++) {
                        bytes[i1] = byteList.get(i1);
                    }

                    String s2 = BytesUtils.bytesToHex(bytes);
                    Log.d(TAG, "anlzData: " + s2);

                    byte b1 = bytes[1];
                    byte deviceNumber = bytes[2];
                    if (b1 == 0x00 && (deviceNumber == (byte)0x04 || deviceNumber == (byte)0x05)) {
                        choiseData(bytes);
                    }
                }
            }
            //如果不是7e，并且此时缓冲区不为空，说明是中间数据
            else if (!byteBufferList.isEmpty()) {
                byteBufferList.add(data);
            }
        }*/
    }

    /**
     * 处理数据
     * @param datas
     */
    private void choiseData(byte[] datas){
        try {
            String s = BytesUtils.bytesToHex(datas);
            Log.i(TAG, "choiseData: " + s);

            byte deviceNumber = datas[2];
            byte dataOrder = datas[3];

            if(deviceNumber == 0x04 && dataOrder == 0x10){//报站信息
                get_AL_Info(datas);
            } else if(deviceNumber == 0x05 && dataOrder == 0x10){//站点信息，报站信息
                getLineList(datas);
            } else if(dataOrder == 0x16){//扩展信息
                String hex = BytesUtils.byteToHex(datas[4]);
                if ("02".equals(hex)){//首末班时间
                    getSiteTime(datas);
                }
            }
            /*byte cmd = datas[3];
            String cmdHex = BytesUtils.byteToHex(cmd);
            switch (cmdHex){
                case "10"://站点信息，报站信息
                    byte[] lineByte = BytesUtils.SubByte(datas, 4, 12);//线路号
                    String lineHex = BytesUtils.bytesToHex(lineByte);
                    d("lineHex->"+lineHex);
//                    lineHex = removeZero(lineHex);
//                    d("lineHex->"+lineHex);
                    String lineNum = StringUtils.hexStringToString(lineHex);
                    d("lineNum->"+lineNum);
                    boolean messyCode = StringUtils.isMessyCode(lineNum);
                    if (!messyCode){
                        getLineList(datas,lineNum);
                    }else {
                        get_AL_Info(datas);
                    }
                    break;
                case "16"://扩展信息
                    String hex = BytesUtils.byteToHex(datas[4]);
                    if ("02".equals(hex)){//首末班时间
                        getSiteTime(datas);
                    }
                    break;
            }*/
        } catch (Exception e) {
            Log.e(TAG, "choiseData: ", e);
            e.printStackTrace();
            if (agreementListener!=null){
                agreementListener.agreementFailed();
            }
        }
    }

    private String removeZero(String string){
        String s = string;
        while (s.endsWith("0")) {
            s = s.substring(0,s.length() - 1);
        }
        if(s.length() % 2 != 0){
            s += "0";
        }
        return s;
    }

    private void getSiteTime(byte[] data){
        d("获取站点时间");
        String startH = BytesUtils.byteToHex(data[5]);
        String startM = BytesUtils.byteToHex(data[6]);
        String endH = BytesUtils.byteToHex(data[7]);
        String endM = BytesUtils.byteToHex(data[8]);

        if (agreementListener!=null){
            agreementListener.onSiteTimeResult(startH+":"+startM,endH+":"+endM,"","");
        }
    }

    /**
     * 线路列表解析
     */
    private ArrayList<String> upList=new ArrayList<>();
    private ArrayList<String> downList=new ArrayList<>();
    private String lineNum;
    private void getLineList(byte[] datas) throws Exception{
        d("解析站点列表和线路号");

        byte[] lineByte = BytesUtils.SubByte(datas, 4, 12);//线路号
        String lineHex = BytesUtils.bytesToHex(lineByte);
        d("lineHex->"+lineHex);
        lineHex = removeZero(lineHex);
        d("lineHex->"+lineHex);
        String lineNum = StringUtils.hexStringToString(lineHex);
        d("lineNum->"+lineNum);
        this.lineNum=lineNum;

        byte[] content = BytesUtils.SubByte(datas, 16, datas.length - (4 + 12 + 2));//减去 消息头+线路号+消息尾 的长度
        Queue<Byte> byteQueue = new LinkedBlockingQueue<>();
        for (byte aByte : content) {
            byteQueue.add(aByte);
        }

        Byte upDown = byteQueue.poll();
        Byte total = byteQueue.poll();
        int count = BytesUtils.hex16to10(BytesUtils.byteToHex(total));

        List<String> siteList = new ArrayList<>();
        int tag = 0;
        int length = 0;
        whileQueue:while (!byteQueue.isEmpty()) {
            switch (tag) {
                case 0:
                    Byte poll = byteQueue.poll();
                    d("文字编码：" + poll);
                    tag = 1;
                    break;
                case 1:
                    length = BytesUtils.hex16to10(BytesUtils.byteToHex(byteQueue.poll()));
                    d("长度：" + length);
                    if(length > byteQueue.size()){
                        break whileQueue;
                    }
                    tag = 2;
                    break;
                case 2:
                    byte[] bytes = new byte[length];
                    for (int i = 0; i < bytes.length; i++) {
                        bytes[i] = byteQueue.poll();
                    }
                    String s = StringUtils.hexStringToString(BytesUtils.bytesToHex(bytes));
                    d("站点名：" + s);
                    siteList.add(s);
                    tag = 0;
                    break;
            }
        }

        if(upDown == 0x00){
            d("添加到上行列表");
            //上行
            upList.clear();
            upList.addAll(siteList);
            if(agreementListener != null){
                //站点列表
                agreementListener.onSiteListResult(upList);
                agreementListener.onLineInfoResult(new String[]{lineNum,upList.get(0),upList.get(upList.size()-1)});
            }
        } else if(upDown == 0x01){
            d("添加到下行列表");
            //下行
            downList.clear();
//            downList.addAll(anzLineList(datas));
            downList.addAll(siteList);
            if(agreementListener != null){
                //站点列表
                agreementListener.onSiteListResult(downList);
                agreementListener.onLineInfoResult(new String[]{lineNum,downList.get(0),downList.get(downList.size()-1)});
            }
        }

//        byte lineType = datas[16];
//        if (lineType == 0x00){
//            //上行
//            upList.clear();
//            upList.addAll(anzLineList(datas));
//            if(agreementListener != null){
//                //站点列表
//                agreementListener.onSiteListResult(upList);
//                agreementListener.onLineInfoResult(new String[]{lineNum,upList.get(0),upList.get(upList.size()-1)});
//            }
//        }else if (lineType == 0x01){
//            //下行
//            downList.clear();
//            downList.addAll(anzLineList(datas));
//        }
//        isUpdate=true;
    }

    private List<String> anzLineList(byte[] datas) throws Exception{
        List<String> lines=new ArrayList<>();
        byte[] subBytes = BytesUtils.SubByte(datas, 18, datas.length - 20);
//        e("subBytes->"+BytesUtils.bytesToHex(subBytes));
        for (int i = 0; i < subBytes.length; i++) {
            byte textType = subBytes[i];//字库类型
            i=i+1;
            byte dataLength = subBytes[i];//站点信息长度
            Integer length = Integer.valueOf(BytesUtils.byteToHex(dataLength), 16);
            byte[] lineName=new byte[length];
            for (int j = 0; j < length; j++) {
                i=i+1;
                lineName[j]=subBytes[i];
            }
            String lineNameHex = BytesUtils.bytesToHex(lineName);
            String lineSite = StringUtils.hexStringToString(lineNameHex);
//            e("lineName->"+lineSite);
            lines.add(lineSite);
        }
        return lines;
    }

    /**
     * 到、离站信息
     * @param datas
     */
    private void get_AL_Info(byte[] datas){
        //到站离站信息
        d("到站离站信息");

        byte up_down = datas[5];//上下行 上行 0x00；下行 0x01
        int currentSiteIndex = BytesUtils.hex16to10(BytesUtils.byteToHex(datas[6]));
        byte inOut = datas[7];//进出站 进站0x00；出站 0x01
        Log.d(TAG, "get_AL_Info: 站点号：" + currentSiteIndex);
        Log.d(TAG, "get_AL_Info: 进出站：" + inOut);

        byte data = datas[12];
        int i = BytesUtils.hex16to10(BytesUtils.byteToHex(data));
        byte[] bytes = BytesUtils.SubByte(datas, 13, i);
        String s = StringUtils.hexStringToString(BytesUtils.bytesToHex(bytes));
        Log.d(TAG, "get_AL_Info: 站点名：" + s);

        //更新到、离站信息
        SiteInfoBean siteInfoBean = new SiteInfoBean();
        siteInfoBean.setSiteName("");

        if(inOut == (byte)0x00){//到站
            siteInfoBean.setStutas(1);
            siteInfoBean.setIndex(currentSiteIndex - 1);
        } else if(inOut == (byte)0x01){
            siteInfoBean.setStutas(0);
            siteInfoBean.setIndex(currentSiteIndex);
        }
        //更新显示
        if (agreementListener!=null){
            agreementListener.onSiteTipResult(siteInfoBean);
        }
    }

    private void d(String log){
        Log.d(TAG, log);
//        LogUtils.d(TAG,log);
    }

    private void e(String log){
        Log.e(TAG, log );
    }


    AtomicInteger atomicInteger = new AtomicInteger(1);
    public void test(){
        new Thread(() -> {
//            File file = new File(Environment.getExternalStorageDirectory(),"321321.txt");
//            Log.d(TAG, "test: " + file.length());
//            List<String> strings = FileIOUtils.readFile2List(file);
//            for (String string : strings) {
//                String s = string.replaceAll(" ", "");
//                Log.d(TAG, "test: " + s);
//                byte[] bytes = BytesUtils.hexToByteArray(s);
//                anlzData(bytes);
//            }
            if (atomicInteger.get() > 3) {
                atomicInteger.set(1);
            }
            File file = new File(Environment.getExternalStorageDirectory(),atomicInteger.get() + ".txt");
            Log.d(TAG, "读取文件：" + file.getName() + " --- " + file.length());
            List<String> strings = FileIOUtils.readFile2List(file);
            for (String string : strings) {
                String s = string.replaceAll(" ", "");
                Log.d(TAG, "test: " + s);
                byte[] bytes = BytesUtils.hexToByteArray(s);
                anlzData(bytes);
            }
            atomicInteger.incrementAndGet();
        }).start();
    }

    public void testSiteList(){
        new Thread(() -> {
            File file = new File(Environment.getExternalStorageDirectory(),"456.txt");
            List<String> strings = FileIOUtils.readFile2List(file);
            for (String string : strings) {
                if(!string.contains("<---")){
                    continue;
                }

                String[] split = string.split("<---");
                if(split.length != 2){
                    continue;
                }
                String s = split[1];
                byte[] bytes = BytesUtils.hexToByteArray(s);
                anlzData(bytes);
            }

            // 7e0005
            // 10
            // 323032c2b700000000000000
            // 01300106cbabbdd6b4e50108ccabc6bdd7afb4e5010cd0cfbcd2d3aab4e5c4cfbfda010cd0cfbcd2d3aab4e5b1b1bfda0108c0eed7afc4cfbfda010ac0eed7afcec0c9fad4ba0108cdf5bcd2cef1b4e50108cee2b4f2d7afb4e5010ac7b0cdc1c3c5c2a5b4e5010abaf3cdc1c3c5c2a5b4e50108c2e2d7d3cdf5b4e50108c8fdb0d9bba7b4e50108cbc4b0d9bba7b4e5010cbaf3bcd2cde5b4e5c4cfbfda0104baecc3ed0106baf3bcd2cde50106c3a9b2ddb5ea0108c3a9b2ddb5eab1b10106b6feb0d9bba70106c6f0bad3cdcd0108c7afcdcdc2b7bfda0108cbabb0b2d0c2b3c7010ed2b6cdcdc2b7d5fed6b4b7a8b6d30104b3d8cdcd0104d5d4cdcd0104c1f5d7af0106d1c7ccabb3c70106bac3b0d9c4ea0108beadceb3bcd2bed30108d0e3cbaebeadb5e40108b8bbc0f6bcced4b0010ab2bdd0d0bdd6cef7bfda0104b9e3b3a10108d0c2bbaabcced4b00106bcc6c9fabed60104c8fdd6d0010cced2bcd2b9abb7bbd0a1c7f80114d0c2b3c7b9e3b3a1b1e3c3f1b7fecef1d6d0d0c4010cb9e6bbaed5b9c0c0d6d0d0c4010acdf5d6b9cef1b4e5bfda010ccec4bbafd2d5caf5d6d0d0c4010cd0c2bbaab4f3bdd6cef7bfda0106c6fbc5e4b3c7010ccdfbd4cbb9dbb3b1d0a1c7f80106b9f9d0c1d7af0106c7e0baa3b3c70108bad3c4cfcee2b4e50108c1e8bcd2cee2b4e5597e

            // 7e0005 [0 - 2]
            // 10 命令 [3]
            // 313033c2b700000000000000 线路号 [4 - 15]
            // 00 //方向标志 [16]
            // 16 //块数 [17]
            // 01 //字库类型 简体中文
            // 0a 内容长度
            // b6abb2bfb0ecb9abc7f8
            // 01
            // 08
            // baf3dbe0c2b7bfda
            // 01
            // 08
            // d4f2d0ecb9e3b3a1
            // 01
            // 0e
            // b8c7c9bdd5f2d5feb8aed0c2b4e5
            // 01
            //
            // 06b8dfbafeb4e5
            // 01
            // 10
            // c1fab8a3bbfab5e7bdbbd2d7cad0b3a1
            // 01
            // 06
            // b9e2c3f7c7c5
            // 0108d4b4b8a3bba8d4b00104cbabdbe00106b4f3c3fbb3c70104d7cfd1f4010ebfb5b3dbd0c2b0cdcabfb9abcbbe0106b9c5ccefc2b70110d3dac9bda3a8d0adbacdd2bdd4baa3a90108cedac9bdc2b7bfda0108cedac9bdc7c5cef7010ccedac9bdcfe9d3ecc2b7bfda0104b7efceb2010bc8dac7c8bdf5bdad41c7f80108c8dac7c8bdf5bdad0104cef7bad3010abdf0c5a3c9bdb9abd4b0257e

//                String s = "7e 00 05 10 313033c2b70000000000000
//                00
//                016010ab6abb2bfb0ecb9abc7f80108baf3dbe0c2b7bfda0108d4f2d0ecb9e3b3a1010eb8c7c9bdd5f2d5feb8aed0c2b4e50106b8dfbafeb4e50110c1fab8a3bbfab5e7bdbbd2d7cad0b3a10106b9e2c3f7c7c50108d4b4b8a3bba8d4b00104cbabdbe00106b4f3c3fbb3c70104d7cfd1f4010ebfb5b3dbd0c2b0cdcabfb9abcbbe0106b9c5ccefc2b70110d3dac9bda3a8d0adbacdd2bdd4baa3a90108cedac9bdc2b7bfda0108cedac9bdc7c5cef7010ccedac9bdcfe9d3ecc2b7bfda0104b7efceb2010bc8dac7c8bdf5bdad41c7f80108c8dac7c8bdf5bdad0104cef7bad3010abdf0c5a3c9bdb9abd4b0257e";

//                String s = "7e000410050001010200010142b3cbbfcdc3c7a3accfc2d2bbd5bebad3c4cfcee2b4e5a1a3d4f6c7bfb0b2c8abd2e2cab6a3accffbb3fdbbf0d4d6d2febbbca3acb9b9bda8bacdd0b3c9e7bbe1a1a35a7e";
//                String s = "7e000510323032c2b70000000000000001300106cbabbdd6b4e50108ccabc6bdd7afb4e5010cd0cfbcd2d3aab4e5c4cfbfda010cd0cfbcd2d3aab4e5b1b1bfda0108c0eed7afc4cfbfda010ac0eed7afcec0c9fad4ba0108cdf5bcd2cef1b4e50108cee2b4f2d7afb4e5010ac7b0cdc1c3c5c2a5b4e5010abaf3cdc1c3c5c2a5b4e50108c2e2d7d3cdf5b4e50108c8fdb0d9bba7b4e50108cbc4b0d9bba7b4e5010cbaf3bcd2cde5b4e5c4cfbfda0104baecc3ed0106baf3bcd2cde50106c3a9b2ddb5ea0108c3a9b2ddb5eab1b10106b6feb0d9bba70106c6f0bad3cdcd0108c7afcdcdc2b7bfda0108cbabb0b2d0c2b3c7010ed2b6cdcdc2b7d5fed6b4b7a8b6d30104b3d8cdcd0104d5d4cdcd0104c1f5d7af0106d1c7ccabb3c70106bac3b0d9c4ea0108beadceb3bcd2bed30108d0e3cbaebeadb5e40108b8bbc0f6bcced4b0010ab2bdd0d0bdd6cef7bfda0104b9e3b3a10108d0c2bbaabcced4b00106bcc6c9fabed60104c8fdd6d0010cced2bcd2b9abb7bbd0a1c7f80114d0c2b3c7b9e3b3a1b1e3c3f1b7fecef1d6d0d0c4010cb9e6bbaed5b9c0c0d6d0d0c4010acdf5d6b9cef1b4e5bfda010ccec4bbafd2d5caf5d6d0d0c4010cd0c2bbaab4f3bdd6cef7bfda0106c6fbc5e4b3c7010ccdfbd4cbb9dbb3b1d0a1c7f80106b9f9d0c1d7af0106c7e0baa3b3c70108bad3c4cfcee2b4e50108c1e8bcd2cee2b4e5597e";

//                String s =
//                        "7e000510323033c2b700000000000000002c0106c6fbb3b5d5be0104d5d4d7af0108bdf5d0e5bcd2d4b0010abac3b0d9c4eacef7c3c5010accecc7d9cde5"
//                        + "d0a1c7f80108d2e6c3f1d2f8d0d00108cee5d2bbb9abd4b0010ccae7d1f4b4f3bdd6cef7bfda0108cfe3b8f1bdf1b5e40104c8fdd6d00106bcc6c9fabed6"
//                        + "0108d0c2bbaabcced4b00104b6fed6d00106d7d4d4dab3c70106bdbbcda8bed60106d6d0d2bdd4ba0108caa2b4efb2acb9ac010ab2bdd0d0bdd6b6abbfda"
//                        + "0108bdf0b6a6b8aedba10108cfe3cee4c2b7bfda0104cee5d0a1010ec9d0c6b7d6aebcd2bda8b2c4b3c70108b8beb2fad2bdd4ba0106b6abd1d3cbc20106"
//                        + "cef7d1d3cbc2010cbfc9bfdabfc9c0d6b9abcbbe0114eac9c0cab5e7c1a6b5e7d7d3d3d0cfdeb9abcbbe0108b8a3b0b2bcd2d4b00106c0eed0c1d7af0106"
//                        + "baf3c2edb7bf0106cdb7b0d9bba7010ac7b0c2edb7bfb4e5bfda0106c1f9b0d9bba70108b8bbc3f1bcd2d4b0010ccee5b0d9bba7cec0c9fad4ba0104cee5"
//                        + "d6d0010ccee5b0d9bba7b2cbcad0b3a10104b2dcd7af0106b8dfd0c1d7af0104d6dcd7af0106cfe3b3c7cdcd0106cfe3b4bbd3aa0108c4cfcaafd0c1d7af"
//                        + "0106b1b1cef1cdcd4e7e";

//                String s = "7e00041005000a000200010116b3cbbfcdc3c7a3accaa2b4efb2acb9acb5bdc1cba3a1367e";

            // 7e000410
            // 05
            // 00
            // 0a
            // 000200010116b3cbbfcdc3c7a3accaa2b4efb2acb9acb5bdc1cba3a1367e
//            String s = "7E 00 " +//[1],[1]
//                    "04 " +//设备编号[1]
//                    "10 " +//数据指令[1]
//                    "05 " +//线路号[12]
//                    "00 " +//线路方向[1]
//                    "02 01 02 00 01 01 42 B3 CB BF CD C3 C7 A3 AC CF C2 D2 BB D5 BE BD F5 D0 E5 BC D2 D4 B0 A1 A3 D4 F6 C7 BF B0 B2 C8 AB D2 E2 CA B6 A3 AC CF FB B3 FD BB F0 D4 D6 D2 FE BB BC A3 AC B9 B9 BD A8 BA CD D0 B3 C9 E7 BB E1 A1 A3 31 7E";

//            String s = "7E 00 " +
//                    "05 " +//设备编号
//                    "10 " +//数据指令
//                    "31 C2 B7 CF C2 D0 D0 00 00 00 00 00 " +//线路号
//                    "00 " +//方向标志
//                    "1B 01 06 C6 FB B3 B5 D5 BE 01 04 D5 D4 D7 AF 01 08 BD F5 D0 E5 BC D2 D4 B0 01 06 BA C3 B0 D9 C4 EA 01 08 BE AD CE B3 BC D2 BE D3 01 0A " +
//                    "D6 D0 D2 E2 BC D2 BE DF B3 C7 01 0A BA EC D0 C7 C3 C0 BF AD C1 FA 01 0C BB E3 C3 C0 BC D2 BE D3 B9 E3 B3 A1 01 08 EF F3 BF DA C2 B7 BE DA 01 0A D0 C2 BF AA BD D6 B6 AB BE DA 01 0A BB AA " +
//                    "BB E3 BC D2 BE DF B3 C7 01 0A B8 AE C7 B0 BD D6 B6 AB BF DA 01 0C D0 CA CA D4 D1 AA B9 DC D2 BD D4 BA 01 06 D7 D4 D4 DA B3 C7 01 04 B6 FE D6 D0 01 08 D0 C2 BB AA BC CE D4 B0 01 06 BC C6 " +
//                    "C9 FA BE D6 01 04 C8 FD D6 D0 01 0C CE D2 BC D2 B9 AB B7 BB D0 A1 C7 F8 01 14 D0 C2 B3 C7 B9 E3 B3 A1 B1 E3 C3 F1 B7 FE CE F1 D6 D0 D0 C4 01 0C B9 E6 BB AE D5 B9 C0 C0 D6 D0 D0 C4 01 0A " +
//                    "CD F5 D6 B9 CE F1 B4 E5 BF DA 01 0C CE C4 BB AF D2 D5 CA F5 D6 D0 D0 C4 01 0C D0 C2 BB AA B4 F3 BD D6 CE F7 BF DA 01 0C D4 CB BA D3 BC D2 D4 B0 C2 B7 BF DA 01 0A B4 F3 C2 DE CD CD C2 B7 " +
//                    "BF DA 01 06 C6 FB B3 B5 D5 BE 2B 7E";
//            s = s.replaceAll(" ","").trim();
//            byte[] bytes = BytesUtils.hexToByteArray(s);
//            Log.e(TAG, "run: " + s);
//            try {
//                anlzData(bytes);
//            } catch (Exception e) {
//                Log.e(TAG, "run: ", e);
//            }


//            File whileQueue


//                anlzData(bytes);
                /*File fileByPath = new File(Environment.getExternalStorageDirectory(),"LS.txt");

                Log.d(TAG, "run: 读取：" + fileByPath.getPath());
                List<String> dataList = new ArrayList<>();
                List<String> strings = FileIOUtils.readFile2List(fileByPath);
                for (String string : strings) {
                    string = string.replaceAll(" ","");
                    if(TextUtils.isEmpty(string)){
                        continue;
                    }
                    if(string.contains("GetExecuteResult")){
                        continue;
                    }
                    if(string.contains("|")){
                        String[] split = string.split("\\|");
                        dataList.add(split[0]);
                    }
//                    if (string.startsWith("(") || string.startsWith("Tot")
//                            || string.startsWith("The") || string.startsWith("Group")
//                            || string.startsWith("CF") || string.startsWith("<")
//                            || string.startsWith("opendir") || string.startsWith("==")
//                            || string.startsWith("AudioLock")|| string.startsWith("do")
//                            || string.startsWith("[") || string.startsWith("stationName")) {
//                        continue;
//                    }
//                    dataList.add(string);
                }

                for (String s : dataList) {
                    try {
                        Log.e(TAG, "----run:" + s);
                        byte[] bytes = BytesUtils.hexToByteArray(s);
                        anlzData(bytes);

                        Thread.sleep(150);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Log.e(TAG, "run: " + e.getMessage());
                    }
                }*/
        }).start();
    }

    public void testNext(){
        String[] strings = {
                "315dd1e0b6bcd0c2b4e5357f7f0101130000137f7f0002100102010113c7aecdfad5f25b3230315dd1e0b6bcd0c2b4e5367f7f0102130000107f7f000310",
                "0102010113c7aecdfad5f25b3230315dd1e0b6bcd0c2b4e5377f7f0103130000117f7f0001100102010113c7aecdfad5f25b3230315dd1e0b6bcd0c2b4e5",
                "357f7f0101130000137f7f0002100102010113c7aecdfad5f25b3230315dd1e0b6bcd0c2b4e5367f7f0102130000107f7f0003100102010113c7aecdfad5",
                "f25b3230315dd1e0b6bcd0c2b4e5377f7f0103130000117f7e00041005000101020001012eb3cbbfcdc3c7a3accfc2d2bbd5bed2e5beaeb6aba1a3cec4c3",
                "f7cabcd3dad0c4a3acb0b2c8abbcf9d3dad0d0a1a37e7e7e0104120000177e7e000410050002000200010114b3cbbfcdc3c7a3acd2e5beaeb6abb5bdc1cb",
                "a3a13e7e7e0104120000177e7e0005160500167e7e00041005000201020001012cb3cbbfcdc3c7a3accfc2d2bbd5bed2e5beaea1a3cec4c3f7cabcd3dad0",
                "c4a3acb0b2c8abbcf9d3dad0d0a1a3627e7e0104120000177e7e0005160501177e7e000410050003000200010112b3cbbfcdc3c7a3acd2e5beaeb5bdc1cb",
                "a3a1247e7e0104120000177e7e00041005000301020001012fb3cbbfcdc3c7a3accfc2d2bbd5be393338d7dcd5bea1a3cec4c3f7cabcd3dad0c4a3acb0b2",
                "c8abbcf9d3dad0d0a1a3157e7e0104120000177e7e0005160500167e7e000410050004000200010115b3cbbfcdc3c7a3ac393338d7dcd5beb5bdc1cba3a1",
                "517e7e0104120000177e7e000410050004010200010130b3cbbfcdc3c7a3accfc2d2bbd5bed0cbd2b5bcd2d4b0a1a3cec4c3f7cabcd3dad0c4a3acb0b2c8",
                "abbcf9d3dad0d0a1a3297e7e0104120000177e7e000410050005000200010116b3cbbfcdc3c7a3acd0cbd2b5bcd2d4b0b5bdc1cba3a1777e7e0104120000"
//                "7e000410050004010200010130b3cbbfcdc3c7a3accfc2d2bbd5bed0cbd2b5bcd2d4b0a1a3cec4c3f7cabcd3dad0c4a3acb0b2c8abbcf9d3dad0d0a1a3297e"
        };

        new Thread(() -> {
            for (String string : strings) {
                byte[] bytes = BytesUtils.hexToByteArray(string);
                anlzData(bytes);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();


//        String s = "7E 00 04 10 09 00 00 00 02 00 01 01 16 B3 CB BF CD C3 C7 A3 AC C1 E9 CF AA C2 B7 BF DA B5 BD C1 CB A3 A1 55 7E ".replaceAll(" ","");
//        byte[] bytes = BytesUtils.hexToByteArray(s);
//        anlzData(bytes);
    }

}
